package com.kiln.juxingqs.ui

import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import client.global.commlib.comm_ui.draw.SpaceItemDecoration
import client.global.commlib.toast.toast
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.WdgzAdapter
import com.kiln.juxingqs.api.bean.WdgzRet
import com.kiln.juxingqs.databinding.ActivityWdgzBinding
import com.kiln.juxingqs.ui.vm.WdgzViewModel
import com.kiln.juxingqs.BaseMiddleActivity
//我的竞拍
class WdgzActivity :
    BaseMiddleActivity<WdgzViewModel, ActivityWdgzBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_wdgz
    }

    val adapter: WdgzAdapter by lazy {
        WdgzAdapter(this@WdgzActivity)
    }

    override fun initViewModel(): WdgzViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel
        binding.recyclerView.adapter = adapter
        binding.emptyLayout.setOnClickListener {
            viewModel.pageOne.value = 0
            viewModel.holdOne()
        }
        binding.recyclerView.addItemDecoration(SpaceItemDecoration(1))
    }

    override fun initData() {
        viewModel.pageOne.value = 0
        viewModel.holdOne()
    }

    override fun observerUI() {
        viewModel.callOne.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    val list =
                        it.data?.collect_list as MutableList<WdgzRet.Collect?>?
                    if (viewModel.pageOne.value == 1) {
                        adapter.submitList(list)
                    } else
                        adapter.appendList(list)
                    if (list != null && list.size > 0) {
                        if (list.size < 20) {
                            adapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (adapter.itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        adapter.hasMoreState.value = false
                    }
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })
    }

    fun searchClick() {

        if (viewModel.searchVal.value.isNullOrEmpty()) {
            toast("请输入关键词")
            return
        }
        viewModel.pageOne.value = 0
        viewModel.holdOne()
    }
}